//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.utils.basehelper.phone;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

public final class DesensitizationUtils {
    private static String phoneMatch = "^[1][3-9]\\d{9}$|^([5|6|9])\\d{7}$|^[0][9]\\d{8}$|^[6]\\d{7}$";

    public static String getDesensitizationName(String name) {
        return StringUtils.isBlank(name) ? name : "*" + name.substring(1);
    }

    public static String getDesensitizationPhone(String phone) {
        if (Objects.isNull(phone)) {
            return null;
        } else if (StringUtils.isBlank(phone)) {
            return phone;
        } else {
            StringBuilder stringBuilder = new StringBuilder(phone.length());
            char[] chars = phone.toCharArray();
            int i = 0;

            for(int size = chars.length; i < size; ++i) {
                if (i < 7 && i > 2) {
                    stringBuilder.append('*');
                } else {
                    stringBuilder.append(chars[i]);
                }
            }

            return stringBuilder.toString();
        }
    }

    public static boolean getPhoneNumberLegaled(Long phone) {
        if (phone == null) {
            return false;
        } else {
            return phone >= 13000000000L && phone <= 19999999999L;
        }
    }

    public static boolean getPhoneNumberLegaled(String phone) {
        if (StringUtils.isBlank(phone)) {
            return false;
        } else {
            phone = phone.trim();
            if (phone.contains("-")) {
                if (!phone.startsWith("852") && !phone.startsWith("853")) {
                    return false;
                } else {
                    Pattern p = Pattern.compile(phoneMatch);
                    Matcher m = p.matcher(phone.split("-")[1]);
                    return m.matches();
                }
            } else if (phone.length() != 11) {
                return false;
            } else {
                char[] chars = phone.toCharArray();
                if (chars[0] != '1') {
                    return false;
                } else if (chars[1] >= '3' && chars[1] <= '9') {
                    int i = 2;

                    for(int size = chars.length; i < size; ++i) {
                        char c = chars[i];
                        if (c < '0' || c > '9') {
                            return false;
                        }
                    }

                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    private DesensitizationUtils() {
    }
}
